from naoqi import ALProxy
import time

class buttonListener():
	listening = False
	memProxy = ALProxy("ALMemory","localhost",9559)
	ttsProxy = ALProxy("ALTextToSpeech", "localhost", 9559)
	chestButtonPressed = False
	setup = True

	def __init__(self, interval=0.5):
		self.interval = interval
		self.chestButtonPressed = False
		self.setup = True

	def chestButton(self):
		if (self.memProxy.getData("ChestButtonPressed", 0) == 0.0):
			return False
		return True

	def refresh(self):
		self.reset()
		now = time.time()
		#keep listening for some time
		while time.time() - now < self.interval:
                        #pretty ugly...
			if self.chestButton():
				self.chestButtonPressed = True

        def test(self):
               
                while True:
                        self.refresh()
	
	def reset(self):
		self.chestButtonPressed = False
		
	def getChestButton(self):
		return self.chestButtonPressed
	
	def setListening(self,set):
		self.listening = set

	def leftBumperButton(self):
		if (self.memProxy.getData("LeftBumperPressed", 0) == 0.0):
			return False
		return True

	# THE FUNCTIONS TO SETUP THE NAO
	def setUp(self, color, kickOff):
		self.ttsProxy.say("Choose my team color by pressing, my left bumper button")
		self.ttsProxy.say("My current color is, " + str(color))
		color = self.chooseTeam(color)
		time.sleep(1)
		self.ttsProxy.say("Choose, who has kick off by pressing, my left bumper button")
		self.ttsProxy.say("Team " + str(kickOff) + " has kick off")
		kickOff = self.chooseKickOff(kickOff)
		time.sleep(1)
		self.ttsProxy.say("Choose, my player type by pressing, my left bumper button")
		self.ttsProxy.say("Team color is, " + str(color) + ", Team " + str(kickOff) + " has kick off")
		self.setup = False
		return (color, kickOff)

	def chooseTeam(self, color):
		startTime = time.time()
		while (time.time() - startTime < 5):
			if (self.leftBumperButton()):
				if(color == 0):
					color = 1
				else:
					color = 0
				self.ttsProxy.say("My team color is, " + str(color))
				time.sleep(1)
		return color

	def chooseKickOff(self, kickOff):
		startTime = time.time()
		while (time.time() - startTime < 5):
			if (self.leftBumperButton()):
				if(kickOff == 0):
					kickOff = 1
				else:
					kickOff = 0
				self.ttsProxy.say("Kick off is for team, " + str(kickOff))
				time.sleep(1)
		return kickOff



	def getSetup(self):
		return self.setup
